package cn.itcast.leadnews.controller;

import cn.itcast.leadnews.common.ResponseResult;
import cn.itcast.leadnews.dto.ChannelDTO;
import cn.itcast.leadnews.pojo.AdChannel;
import cn.itcast.leadnews.service.ChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "频道管理controller",tags = "频道管理controller")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {
    @Autowired
    private ChannelService channelService;
    @ApiOperation(value = "根据条件分页查询",notes = " 根据条件  : name 模糊   status 等值   进行查询， 结果以ord升序排序")
    @PostMapping("/list")
    public ResponseResult<List<AdChannel>> findByQuery(@RequestBody ChannelDTO dto){
        return channelService.findByQuery(dto);
    }
    @ApiOperation(value = "保存频道",notes = " 保存频道信息  校验：频道名不能为空，不能大于10个字符，不可以重复   自动填充创建时间，修改时间")
    @PostMapping("/save")
    public ResponseResult<Boolean> save(@RequestBody @Validated AdChannel channel){
        return channelService.insert(channel);
    }
    @ApiOperation(value = "修改频道",notes = " 修改频道信息  校验：频道id不能为空  自动填充修改时间 ,修改要考虑并发使用乐观锁方案修改 version字段")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody AdChannel channel){
        return channelService.update(channel);
    }
    @ApiOperation(value = "删除频道",notes = " 删除频道信息  校验：频道id不能为空   频道状态如果是启用不可以删除   删除采用逻辑删除 flag = 1")
    @GetMapping("/del/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id){
        return channelService.deleteById(id);
    }
}
